﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SFP.Animation2D
{
    public class ImageSequencer
    {
        private Image[] images;
        private Image currentImage;

        private int imageIndex = 0;
        private double iTime = 0;

        public Point Position { get; set; }
        public double Intervall { get; set; }
        public int Width { get { return currentImage.Width; } }
        public int Height { get { return currentImage.Height; } }
        
        public ImageSequencer(Image[] images, double intervall, Point position)
        {
            this.images = images;
            currentImage = images[0];
            Intervall = intervall;
            Position = position;
        }

        public void resetSequence()
        {
            currentImage = images[0];
        }

        public void animate(double time)
        {
            animate(time, false);
        }

        public void animate(double time, bool loop)
        {
            iTime += time;

            if (iTime >= Intervall)
            {
                imageIndex++;
                if (imageIndex >= images.Length)
                {
                    if (loop)
                        imageIndex = 0;
                    else
                        imageIndex = images.Length;
                }
                currentImage = images[imageIndex];
                iTime = iTime - Intervall;
            }
        }

        public void draw(Graphics graphics)
        {
            graphics.DrawImage(currentImage, Position.X, Position.Y - Height);
        }
    }
}
